package models

import (
	"time"

	"gorm.io/gorm"
)

// User 用户模型
type UserModel struct {
	gorm.Model
	Email       string    `json:"email" csv:"email" gorm:"column:email"`
	Password    string    `json:"-" csv:"password" gorm:"column:password"` // 不在JSON中返回密码
	Token       string    `json:"token" csv:"token" gorm:"column:token"`
	ExpiredAt   time.Time `json:"expired_at" csv:"expired_at" gorm:"column:expired_at"`
	UsageAmount int64     `json:"usage_amount" csv:"usage_amount" gorm:"column:usage_amount"`
}

// TableName 指定表名
func (UserModel) TableName() string {
	return "users"
}

// IsVIP 检查用户是否为VIP（未过期）
func (u *UserModel) IsVIP() bool {
	return u.ExpiredAt.After(time.Now())
}

// GetTotalAmount 获取用户总流量配额
func (u *UserModel) GetTotalAmount() int64 {
	if u.IsVIP() {
		return 1000 * 1024 * 1024 * 1024 // 1TB for VIP
	}
	return 2 * 1024 * 1024 * 1024 // 2GB for free users
}

// AddTime 为用户添加时间
func (u *UserModel) AddTime(duration time.Duration) {
	if u.ExpiredAt.Before(time.Now()) {
		u.ExpiredAt = time.Now()
	}
	u.ExpiredAt = u.ExpiredAt.Add(duration)
	u.UpdatedAt = time.Now()
}
